import React, { createRef, forwardRef, useImperativeHandle } from 'react';
import { Button, message } from 'antd';

const Toast = forwardRef((props, ref) => {
    const [messageApi, contextHolder] = message.useMessage();
    useImperativeHandle(ref, () => ({
        show: (msg: string, type: "info" | "success" | "error" | "warning") => {
            // if (type === "success") {
            //     messageApi.success(msg);
            // }
            // else if (type === "error") {
            //     messageApi.error(msg);
            // }
            // else if (type === "warning") {
            //     messageApi.warning(msg);
            // } else {
            //     messageApi.info(msg);
            // }
            messageApi.open({
                type: type,
                content: msg,
                duration: 1.5,
            });
        }
    }));

    return <>
        {contextHolder}
    </>
})

const ToastRef = createRef<{ show: (msg: string, type: "info" | "success" | "error" | "warning") => {} }>();

export const ToastContain = () => {
    return <Toast ref={ToastRef} />
}

export const showToast = (msg: string, type: "info" | "success" | "error" | "warning") => {
    if (ToastRef.current) {
        ToastRef.current.show(msg, type)
    }
};
